using UnityEngine;
using System.Collections;

public class sPhysics : MonoBehaviour
{
	private static sPhysics instance = null;

	private sPhysics ()
	{
	}

	public static sPhysics Instance
	{
		get
		{
			if (instance == null)
			{
				Debug.Log("instantiate physic helper");
                GameObject go = new GameObject();
                instance = go.AddComponent<sPhysics>();
                go.name = "sPhysics";
			}
			return instance;
		}
	}
	
	Vector3 m_vHitNormal;
	Vector3 m_vHitPoint;
	Collider m_oHitCollider;
	RaycastHit m_oHitInfo;
	
	int LAYER_SCENE;
	int LAYER_COVERS;
	int LAYER_ALL_SCENE;
	int LAYER_SCENE_INDEX;
	int LAYER_COVERS_INDEX;
	
	public void startPhysics()
	{
		LAYER_SCENE_INDEX = 8;
		LAYER_COVERS_INDEX = 9;
		LAYER_SCENE = 1 << LAYER_SCENE_INDEX;
		LAYER_COVERS = 1 << LAYER_COVERS_INDEX;
		LAYER_ALL_SCENE = LAYER_SCENE | LAYER_COVERS;
	}
	
	public Vector3 getLinecastPoint()
	{
		return m_vHitPoint;
	}
	public Vector3 getLinecastNormal()
	{
		return m_vHitNormal;
	}
	public Collider getLinecastCollider()
	{
		return m_oHitCollider;
	}
	
	public bool linecast(Vector3 vFrom, Vector3 vTo, int iLayer)
	{
		if (Physics.Linecast(vFrom, vTo, out m_oHitInfo, iLayer))
		{
			m_vHitPoint = m_oHitInfo.point;
			m_vHitNormal = m_oHitInfo.normal;
			m_oHitCollider = m_oHitInfo.collider;
			return true;
		}
		return false;
	}
	
	public bool linecastVsAll(Vector3 vFrom, Vector3 vTo)
	{
		return linecast (vFrom, vTo, LAYER_ALL_SCENE);
	}
	
	public bool linecastVsEnemies(Vector3 vFrom, Vector3 vTo)
	{
		// TODO implement
		return false;
	}
	
	public bool linecastVsCovers(Vector3 vFrom, Vector3 vTo)
	{
		return linecast (vFrom, vTo, LAYER_COVERS);
	}
}